package org.jjj.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jjj.org.basic.utils.PageList;
import org.jjj.org.basic.utils.ResultAjax;
import org.jjj.org.domain.Employee;
import org.jjj.org.query.EmployeeQuery;
import org.jjj.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 员工
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口",description = "员工的CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询所有员工
     */
    @GetMapping
    @ApiOperation(value = "获取所有员工")
    public ResultAjax getAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return ResultAjax.me().setResult(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    /**
     * 前端请求的新增和修改之后数据保存写到一个方法中
     * 用put请求
     * 操作成功与否，后端要给前端返回信息
     * 封装成一个对象返回：ResultAjax{success:true,message:"操作成功",result:{}}
     */
    @PutMapping
    @ApiOperation(value = "员工的新增和修改")
    public ResultAjax addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                //无id新增方法
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
        } catch (Exception e) {
            //异常就返回操作失败
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("新增修改失败"+e.getMessage());
        }
        return ResultAjax.me();
    }
    /**
     * 前端请求删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除员工")
    public ResultAjax delete(@PathVariable("id") Serializable id){
        try {
            employeeService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
        return ResultAjax.me();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询某员工")
    public ResultAjax selectById(@PathVariable("id") Serializable id){
        try {
            Employee employee = employeeService.selectById(id);
            return ResultAjax.me().setResult(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    /**
     * 分页高级查询
     */
    @PostMapping
    @ApiOperation(value = "分页高级查询")
    public ResultAjax pageList(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> employeePageList = employeeService.queryPage(query);
            return ResultAjax.me().setResult(employeePageList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public ResultAjax batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("后端批量删除失败"+e.getMessage());
        }
        return ResultAjax.me();
    }
}

